package com.xuan.pmh.item;

import java.util.ArrayList;
import java.util.List;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.TextView;

import com.xuan.pmh.R;
import com.xuan.pmh.bean.Option;
import com.xuan.pmh.bean.Question;
import com.xuan.pmh.util.UiUtil;

public class QuestionItem {
	private LayoutInflater inflater;
	private OptionItem optionItem;

	public QuestionItem(LayoutInflater inflater) {
		// TODO Auto-generated constructor stub
		this.inflater = inflater;
		optionItem = new OptionItem(inflater);
	}

	public Question getQuestionEdit(View view) {
		Question q = new Question();
		LinearLayout llOption = (LinearLayout) view.findViewById(R.id.llOption);
		EditText etContent;
		etContent = (EditText) view.findViewById(R.id.etQuestionContent);
		q.options = new ArrayList<Option>();
		q.content = UiUtil.getEditTextString(etContent);
		for (int i = 0; i < llOption.getChildCount(); i++) {
			q.options.add(optionItem.getOptionEdit(llOption.getChildAt(i)));
		}
		return q;
	}

	public View createQuestionEdit(int optionNum, String questionTitle) {
		View view = inflater.inflate(R.layout.item_question_edit, null);
		LinearLayout llOption = (LinearLayout) view.findViewById(R.id.llOption);
		TextView tvTitle = (TextView) view.findViewById(R.id.tvQuestionTitle);
		Button btAddOption = (Button) view.findViewById(R.id.btAddOption);
		Button btDeleteOption = (Button) view.findViewById(R.id.btDeleteOption);

		tvTitle.setText(questionTitle);
		for (int i = 0; i < optionNum; i++) {
			llOption.addView(optionItem.getOptionEdit());
		}
		btAddOption.setTag(llOption);
		btAddOption.setOnClickListener(onclick);
		btDeleteOption.setTag(llOption);
		btDeleteOption.setOnClickListener(onclick);

		return view;
	}

	public View createQuestion(String title, Question q) {
		View view = inflater.inflate(R.layout.item_question_show, null);
		view.setTag(q);
		TextView tvQuestionContent, tvQuestionTitle;
		LinearLayout llOption;
		Spinner spQuestion;
		tvQuestionContent = (TextView) view
				.findViewById(R.id.tvQuestionContent);
		tvQuestionTitle = (TextView) view.findViewById(R.id.tvQuestionTitle);
		llOption = (LinearLayout) view.findViewById(R.id.llOption);
		spQuestion = (Spinner) view.findViewById(R.id.spQuestion);
		tvQuestionContent.setText(q.content);
		tvQuestionTitle.setText(title);

		List<String> opList = new ArrayList<String>();
		for (int i = 0; i < q.options.size(); i++) {
			View optionView = optionItem.getOption(i, q.options.get(i));
			opList.add(String.valueOf((char)('A' + i)));
			llOption.addView(optionView);
		}
		ArrayAdapter<String> adapter = new ArrayAdapter<String>(
				inflater.getContext(), android.R.layout.simple_spinner_item,
				opList);
		spQuestion.setAdapter(adapter);
		return view;
	}

	public int getGrade(View view){
		Question q = (Question) view.getTag();
		Spinner spQuestion = (Spinner) view.findViewById(R.id.spQuestion);
		int optionNum = spQuestion.getSelectedItem().toString().charAt(0)-'A';
		Log.v("position :", "  " + spQuestion.getSelectedItemPosition());
		return q.options.get(optionNum).grade;
	}
	
	OnClickListener onclick = new OnClickListener() {
		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			LinearLayout ll = (LinearLayout) v.getTag();
			switch (v.getId()) {
			case R.id.btAddOption:
				ll.addView(optionItem.getOptionEdit());
				break;
			case R.id.btDeleteOption:
				ll.removeViewAt(ll.getChildCount() - 1);
				break;
			}

		}
	};

}
